package com.fruit.logistics;

import com.fruit.pojo.logistics.*;
import com.fruit.service.logistics.LogisticsService;
import com.fruit.service.logistics.NoSuitableProxyException;
import com.fruit.service.logistics.RouteListener;

/**
 * 抽象快递代理，简化快递代理类的实现
 *
 * @author joe
 * @version 2018.05.15 10:53
 */
public abstract class AbstractLogisticsProxyService implements LogisticsProxyService {
    /**
     * 实现了{@link com.fruit.service.logistics.LogisticsService#register(RouteListener)}的服务
     */
    protected LogisticsService service;

    @Override
    public boolean filter(LogisticsType logistics) {
        return logistics.equals(getProxyType());
    }

    @Override
    public void init(LogisticsService service) {
        this.service = service;
    }

    @Override
    public void register(RouteListener listener) {
        service.register(listener);
    }

    @Override
    public SysLogisticsOrderResponse send(LogisticsType type, SysLogistics logistics) throws NoSuitableProxyException {
        if (!filter(type)) {
            throw new NoSuitableProxyException();
        }
        return send(logistics);
    }

    @Override
    public SysRoute routeNotify(String data, LogisticsType type) throws NoSuitableProxyException {
        if (!filter(type)) {
            throw new NoSuitableProxyException();
        }
        return routeNotify(data);
    }

    @Override
    public SysLogisticsOrderResponse searchSend(LogisticsType type, String orderId) throws NoSuitableProxyException {
        if (!filter(type)) {
            throw new NoSuitableProxyException();
        }
        return searchSend(orderId);
    }

    @Override
    public SysRouteResponse searchRoute(LogisticsType type, String id, boolean isSys) throws NoSuitableProxyException {
        if (!filter(type)) {
            throw new NoSuitableProxyException();
        }
        return searchRoute(id, isSys);
    }

    @Override
    public SysDeliverResponse searchDeliver(LogisticsType type, SysDeliver deliver) throws NoSuitableProxyException {
        if (!filter(type)) {
            throw new NoSuitableProxyException();
        }
        return searchDeliver(deliver);
    }

    @Override
    public SysNewDeliverResponse searchDeliver(LogisticsType type, SysNewDeliver deliver) throws
            NoSuitableProxyException {
        if (!filter(type)) {
            throw new NoSuitableProxyException();
        }
        return searchDeliver(deliver);
    }


    /**
     * 路由通知，请勿调用！！
     *
     * @param data 路由通知内容
     * @return 转换后的系统路由
     * @throws NoSuitableProxyException 当没有合适的快递可以使用时会抛出该异常
     */
    public abstract SysRoute routeNotify(String data);

    /**
     * 查询发快递的结果
     *
     * @param orderId 订单ID（需要与发快递时传递的值一致）
     * @return 发快递接口的调用结果
     * @throws NoSuitableProxyException 当没有合适的快递可以使用时会抛出该异常
     */
    public abstract SysLogisticsOrderResponse searchSend(String orderId);

    /**
     * 查找路由
     *
     * @param id    单号ID
     * @param isSys 是否是系统单号（true表示传入的ID是系统订单号，false表示传入ID时快递运单号）
     * @return 路由结果
     * @throws NoSuitableProxyException 当没有合适的快递可以使用时会抛出该异常
     */
    public abstract SysRouteResponse searchRoute(String id, boolean isSys);

    /**
     * 查询时效
     *
     * @param deliver 查询参数
     * @return 查询结果
     * @throws NoSuitableProxyException 当没有合适的快递可以使用时会抛出该异常
     */
    public abstract SysDeliverResponse searchDeliver(SysDeliver deliver);

    /**
     * 查询时效
     *
     * @param deliver 查询参数
     * @return 查询结果
     * @throws NoSuitableProxyException 当没有合适的快递可以使用时会抛出该异常
     */
    public abstract SysNewDeliverResponse searchDeliver(SysNewDeliver deliver);

    /**
     * 本快递代理类代理的类型
     *
     * @return 代理类型
     */
    public abstract LogisticsType getProxyType();
}
